package com.example.cheeseerpadmin.bus.service.impl;

import com.example.cheeseerpadmin.bus.entity.*;
import com.example.cheeseerpadmin.bus.mapper.BusGoodsMapper;
import com.example.cheeseerpadmin.bus.mapper.BusInportMapper;
import com.example.cheeseerpadmin.bus.mapper.BusSalesMapper;
import com.example.cheeseerpadmin.bus.mapper.BusSalesbackMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cheeseerpadmin.bus.service.BusSalesbackService;
import com.example.cheeseerpadmin.sys.common.WebUtils;
import com.example.cheeseerpadmin.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cheese
 * @since 2022-02-21
 */
@Service
public class BusSalesbackServiceImpl extends ServiceImpl<BusSalesbackMapper, BusSalesback> implements BusSalesbackService {

    @Autowired
    private BusGoodsMapper busGoodsMapper;
    @Autowired
    private BusSalesMapper busSalesMapper;

    @Override
    public void addSalesback(Integer id, Integer number, String remark) {
        //根据销售单id拿到销售单
        BusSales busSales = busSalesMapper.selectById(id);
        busSales.setNumber(busSales.getNumber()-number);
        busSalesMapper.updateById(busSales);
        //根据销售单拿到商品id
        BusGoods busGoods = busGoodsMapper.selectById(busSales.getGoodsid());
        busGoods.setNumber(busGoods.getNumber()+number);
        busGoodsMapper.updateById(busGoods);
        //创建一个退货单对象，然后往里面放值,最后直接插入表中即可
        BusSalesback busSalesback = new BusSalesback();
        SysUser user = (SysUser) WebUtils.getSession().getAttribute("user");
        busSalesback.setCustomerid(busSales.getCustomerid());
        busSalesback.setPaytype(busSales.getPaytype());
        busSalesback.setSalesbacktime(new Date());
        busSalesback.setOperateperson(user.getName());
        busSalesback.setSalebackprice(busSales.getSaleprice());
        busSalesback.setNumber(number);
        busSalesback.setRemark(remark);
        busSalesback.setGoodsid(busSales.getGoodsid());
        getBaseMapper().insert(busSalesback);
    }
}
